/*
 * ResipUtil.cpp
 *
 *  Created on: 2015年6月4日
 *      Author: chuanjiang.zh@qq.com
 */

#include "ResipUtil.h"
#include <iostream>
#include <sstream>
#include "TStringUtil.h"
#include "TStringCast.h"



ResipUtil::ResipUtil()
{
}

ResipUtil::~ResipUtil()
{
}

std::string ResipUtil::toString(const SdpContents& sdp)
{
    const SdpContents::Session& session = sdp.session();
    std::ostringstream oss;
    session.encode(oss);
    std::string strSdp = oss.str();
    return strSdp;
}

bool ResipUtil::isDuplex(const SdpContents& sdp)
{
    const SdpContents::Session& session = sdp.session();
    std::ostringstream oss;
    session.encode(oss);
    std::string strSdp = oss.str();
    size_t pos = strSdp.find("sendrecv");
    if (pos != std::string::npos)
    {
        return true;
    }
    else
    {
        if ((strSdp.find("sendonly") != std::string::npos) && (strSdp.find("recvonly") != std::string::npos))
        {
            return true;
        }
    }
    return false;
}



bool ResipUtil::getVideoAddr(const SdpContents& sdp, Data& ip, uint16_t& port, uint8_t& payload)
{
    typedef SdpContents::Session Session;
    typedef SdpContents::Session::Medium Medium;

    bool found = false;

    const SdpContents::Session& session = sdp.session();

    ip = session.connection().getAddress();

    const SdpContents::Session::MediumContainer& mediaList = session.media();
    SdpContents::Session::MediumContainer::const_iterator it = mediaList.begin();
    for (; it != mediaList.end(); ++ it)
    {
        const SdpContents::Session::Medium& media = *it;
        if (media.name() == "video")
        {
            if (!media.exists("sendrecv") && !media.exists("recvonly"))
            {
                continue;
            }

            const std::list< SdpContents::Session::Connection >& connections = media.getConnections();
            if (!connections.empty())
            {
                const SdpContents::Session::Connection& conn = connections.front();
                ip = conn.getAddress();
            }

            port = media.port();

            const SdpContents::Session::Medium::CodecContainer& codecs = media.codecs();
            Medium::CodecContainer::const_iterator it = codecs.begin();
            for (; it != codecs.end(); ++ it)
            {
                const Session::Codec& codec = *it;
                if (codec.getName() == "H264")
                {
                    payload = codec.payloadType();
                    break;
                }
            }

            found = true;

            break;
        }
    }

    return found;
}

Mime ResipUtil::makeMime(const std::string& mime)
{
    std::string type;
    std::string subtype;
    comn::StringUtil::split(mime, '/', type, subtype);
    return Mime(type.c_str(), subtype.c_str());
}

void ResipUtil::setMime(SipMessage& msg, const std::string& mime)
{
    Mime& mimeType = msg.header(h_ContentType);
    mimeType = makeMime(mime);
}

std::string ResipUtil::getMime(const SipMessage& msg)
{
    const Mime& mimeType = msg.header(h_ContentType);
    return comn::StringCast::toString(mimeType);
}
